﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mime;
using System.Net.Mail;
using SPErrorHandler;
using System.Configuration;
using System.IO;

namespace SendMail
{
    class SendEMail
    {
        //public const string fileLogPath = "WarmUpLog.txt";

        public void SendMailMessage(string bodyText, string subject, string fromMail, string toMail, string smtpServer, string fileLogPath)
        {
            MailMessage message = new MailMessage(fromMail, toMail, subject, bodyText);
            if (File.Exists(fileLogPath))
            {
                // Create  the file attachment for this e-mail message.
                Attachment data = new Attachment(fileLogPath, MediaTypeNames.Application.Octet);
                // Add time stamp information for the file.
                ContentDisposition disposition = data.ContentDisposition;
                disposition.CreationDate = System.IO.File.GetCreationTime(fileLogPath);
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(fileLogPath);
                disposition.ReadDate = System.IO.File.GetLastAccessTime(fileLogPath);
                // Add the file attachment to this e-mail message.
                message.Attachments.Add(data);
                //Send the message.
                data.Dispose();
            }
            SmtpClient client = new SmtpClient(smtpServer);
            try
            {
                client.Send(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", ex.ToString());
                ErrorHandler errHandler = new ErrorHandler();
                errHandler.WriteLogToFile("SmtpErrorLog--------------------------------------------------");
                errHandler.WriteLogToFile(ex.ToString());
                errHandler.WriteLogToFile("--------------------------------------------------------------");
            }
        }

    }
}
